package com.video.video.common;

import java.io.*;
import java.net.Socket;

/**
 * 服务器线程类
 */
public class ServerThread extends Thread {

    //和本线程相关的socket
    Socket socket = null;

    public ServerThread(Socket socket){
        this.socket = socket;
    }
    //线程执行的操作，响应客户端的请求

    @Override
    public void run() {
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        OutputStream os = null;
        PrintWriter pw = null;
        try {
            //3、获取输入流，并读取客户端信息
            is = socket.getInputStream();//字节输入流
            isr = new InputStreamReader(is);//将字节流包装成字符流
            br = new BufferedReader(isr);//为字节流添加缓冲
            String info = null;
            while ((info = br.readLine()) != null){//循环获取客户端信息
                System.out.println("客户端：" + info);
            }

            socket.shutdownInput();//关闭输入流

            //4、获取输出流，响应客户端响应
            os = socket.getOutputStream();
            pw = new PrintWriter(os);
            pw.write("欢迎您");
            pw.flush();//调用flush()方法将缓冲输出

            socket.shutdownOutput();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //关闭资源
            try {
                if(pw!=null)
                    pw.close();
                if(os!=null)
                    os.close();
                if(br!=null)
                    br.close();
                if(isr!=null)
                    isr.close();
                if(is!=null)
                    is.close();
                if(socket!=null)
                    socket.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }
}
